package dragon.modules.system.org.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;

import dragon.core.db.DML;
import dragon.core.exception.ExceptionHandler;
import dragon.core.system.po.SysOrg;
import dragon.core.system.po.SysUser;
import dragon.core.system.role.Roles;
import dragon.core.util.Handler;
import dragon.core.util.StringUtils;
import dragon.modules.system.org.dao.OrgDaoImpl;

public class OrgHandler extends Handler {

	/**
	 * 改变组织机构的状态(停、启用)
	 * 
	 * @throws ExceptionHandler
	 */
	public void changeState() throws ExceptionHandler {
		String ids_str = getParameter("ids").trim();
		String state = getParameter("state").trim();
		if (!StringUtils.isEmpty(ids_str) && !StringUtils.isEmpty(state)) {
			String[] ids = ids_str.split(",");
			new OrgDaoImpl().changeOrgState(ids, state);
		}
		output("t");
	}

	/**
	 * 变更部门父节点
	 * 
	 * @throws ExceptionHandler
	 */
	public void changeParent() throws ExceptionHandler {
		String id = getParameter("id");// 待移动的部门ID
		String target = getParameter("target");// 目标父节点ID
		if ("null".equalsIgnoreCase(target)) {
			target = "0";
		}
		if (StringUtils.isInteger(id) && StringUtils.isInteger(target)) {
			new OrgDaoImpl().changeParent(Integer.parseInt(id), Integer.parseInt(target));
			output(successJSON);
		}
	}

	/**
	 * 用于前台页面中通过combotree的方式调用，用于获取某一节点下的组织机构数据
	 * 
	 * @throws ExceptionHandler
	 */
	@SuppressWarnings("unchecked")
	public void combotree() throws ExceptionHandler {
		String id = getParameter("id");
		String pid = id;
		if (StringUtils.isEmpty(id)) {
			// 判断用户权限
			SysUser user = getUser();
			if (user.inGroup(Roles.Administrators)) {
				// 系统管理员，默认从根节点查找
				pid = "0";
			} else {
				// 普通用户，从其所属组织机构查找
				pid = String.valueOf(user.getOrg());
			}
			user = null;
		}

		// 加载部门
		String orgSql = null;
		if (!StringUtils.isEmpty(id) || "0".equals(pid)) {
			orgSql = "select id,name from " + SysOrg.tableName + " where state='1' and pid=" + Integer.parseInt(pid);
		} else {
			orgSql = "select id,name from " + SysOrg.tableName + " where state='1' and id =" + Integer.parseInt(pid);
		}
		List<Map<String, Object>> orgs = DML.query(orgSql);
		orgSql = null;
		List<Map<String, String>> list = new ArrayList<Map<String, String>>(orgs.size());
		Map<String, String> map = null;
		for (Map<String, Object> org : orgs) {
			map = new HashMap<String, String>();
			map.put("id", org.get("ID").toString());
			map.put("text", org.get("NAME").toString());
			map.put("state", "closed");
			map.put("kind", "0");
			list.add(map);
			map = null;
		}
		orgs = null;
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("", list);
		String result = jsonObject.toJSONString();
		result = result.substring(4, result.length() - 1);
		output(result);

		result = null;
		jsonObject = null;
		list = null;
	}

	/**
	 * 验证编码或名称是否存在
	 * 
	 * @throws Exception
	 */
	public void isExist() throws Exception {
		String id = getParameter("ID");
		String pid = getParameter("PID");
		if (StringUtils.isEmpty(id)) {
			id = "0";
		}
		if (StringUtils.isEmpty(pid)) {
			pid = "0";
		}
		String type = getParameter("type");
		String value = getParameter("_CURRENTVALUE");
		if (StringUtils.isInteger(id) && StringUtils.isInteger(pid) && !StringUtils.isEmpty(value)) {
			String name = "";
			String code = "";
			if ("code".equals(type)) {
				code = value;
			} else {
				name = value;
			}
			output(!new OrgDaoImpl().isExist(Integer.parseInt(id), Integer.parseInt(pid), code, name));
		}
	}

}
